/**
 * 
 */
package com.jm.web.form.table;

import java.io.IOException;

import javax.servlet.jsp.JspWriter;

import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.web.data.ReturnData;
import com.jm.web.form.Button;
import com.jm.web.form.Paging;
import com.jm.web.form.Separate;
import com.jm.web.form.Toolbar;
import com.jm.web.form.support.FormItem;
import com.jm.web.tags.table.TableTag;
import com.jm.web.tags.table.TfootTag;
/**
 * @author yanjie
 *
 * 2011/9/22 下午1:29:57
 */
public class Tfoot extends FormItem{
	public final static String TAG_ITEMNAME = "TFOOT";
	/**
	 * @param tag
	 */
	public Tfoot() {}
	
	public void init() {
		this.setNodeName("tfoot");
		super.init();
	}
	
	
	@Override
	public void onOutItemEnd(JspWriter writer) {
		//绘制分页
		if(false && !this.getBaseTag().getPage().isExport() && this.getBaseTag() instanceof TfootTag){
			TfootTag tfootTag = (TfootTag)this.getBaseTag();
			TableTag tableTag = tfootTag.getTableTag();
			Table table = tableTag.getTable();
			ReturnData ret = this.getBaseTag().getPage().getReturnData();
			Paging paging = (Paging)ret.GetParamValue(StringHelper.Format("paging_%1$s",table.getId()));
			
			/**
			 * 
			 * 
			 * **/
			if(paging != null){
				Separate sep = new Separate();
				sep.InitTag(this.getBaseTag());
				
				
				StringBuilderEx sb = new StringBuilderEx();
				sb.Append("<tr style=\"background-color: #D5E2F2;height:17px;\" class=\"tt9\"><th colspan=\"100\" style=\"height:17px;\">");
				sb.Append("<table cellspacing=\"0\" border=\"0\" class=\"listing-noborder\" width=\"100%\"><tbody><tr>");
				
//				sb.Append("<td>");
//				sb.Append("</td>");
				
				sb.Append("<td align=\"left\" style=\"width:100%;float:left;\" nowrap=\"nowrap\">");
				sb.Append("<table cellspacing=\"0\" border=\"0\" class=\"listing-noborder\"><tbody><tr>");
				
				boolean bfirst = !(paging.getCurrentPage() <= 1);
				sb.Append("<td style=\"padding-left:0px;padding-right:0px;\" class=\"%1$s\">",!bfirst ? "ax-item-disabled" : "");
				Button first = Toolbar.getNewButton("","paging_frist","paging_frist","ax-tbar-page-first xt-btn-icon",null);
				first.setOnClick(StringHelper.Format("doPaging(%1$s,%2$s);return false;",1,paging.toJsonString().replaceAll("\"","'")));
				if(!bfirst){
					first.setEnab(false);
				}
				sb.Append(first.toButtonString());
				sb.Append("</td>");
				
				boolean bpre = !(paging.getCurrentPage() <= 1);
				sb.Append("<td style=\"padding-left:0px;padding-right:0px;\" class=\"%1$s\">",!bpre ? "ax-item-disabled" : "");
				Button pre = Toolbar.getNewButton("","paging_prev","paging_prev","xt-btn-icon ax-tbar-page-prev",null);
				pre.setOnClick(StringHelper.Format("doPaging(%1$s,%2$s);return false;",paging.getCurrentPage()-1,paging.toJsonString().replaceAll("\"","'")));
				if(!bpre){
					pre.setEnab(false);
				}
				sb.Append(pre.toButtonString());
				sb.Append("</td>");
				
				sb.Append("<td style=\"padding-left:0px;padding-right:0px;\">");
				sb.Append(sep.toOutString());
				sb.Append("</td>");
				
				
				sb.Append("<td style=\"font: 12px tahoma,arial,helvetica,sans-serif;padding-left:0px;padding-right:0px;\">");
				sb.Append("當前 ");
				sb.Append("<input type=\"text\" name=\"pageing_page\" onkeydown=\"if(event.keyCode==13)doPaging(this.value,%2$s);\" value=\"%1$s\" realvalue=\"%1$s\" style=\"font: 12px tahoma,arial,helvetica,sans-serif;height: 14px;line-height: 14px;vertical-align: middle;width: 30px;\"/>",paging.getCurrentPage(),paging.toJsonString().replaceAll("\"","'"));
				sb.Append("/%1$s 頁",paging.getTotalPage());
				sb.Append("</td>");
				
				sb.Append("<td style=\"padding-left:0px;padding-right:0px;\">");
				
				sb.Append(sep.toOutString());
				sb.Append("</td>");
				
				boolean bnext = paging.getCurrentPage() < paging.getTotalPage();
				sb.Append("<td style=\"padding-left:0px;padding-right:0px;\" class=\"%1$s\">",!bnext ? "ax-item-disabled" : "");
				Button next = Toolbar.getNewButton("","paging_next","paging_next","xt-btn-icon ax-tbar-page-next",null);
				next.setOnClick(StringHelper.Format("doPaging(%1$s,%2$s);return false;",paging.getCurrentPage()+1,paging.toJsonString().replaceAll("\"","'")));
				if(!bnext){
					next.setEnab(false);
				}
				sb.Append(next.toButtonString());
				sb.Append("</td>");
				
				boolean blast = paging.getTotalPage() > 1 && paging.getCurrentPage() != paging.getTotalPage();
				sb.Append("<td style=\"padding-left:0px;padding-right:0px;\" class=\"%1$s\">",!blast ? "ax-item-disabled" : "");
				Button last = Toolbar.getNewButton("","paging_last","paging_last","xt-btn-icon ax-tbar-page-last",null);
				last.setOnClick(StringHelper.Format("doPaging(%1$s,%2$s);return false;",paging.getTotalPage(),paging.toJsonString().replaceAll("\"","'")));
				if(!blast){
					last.setEnab(false);
				}
				sb.Append(last.toButtonString());
				sb.Append("</td>");
				
				sb.Append("<td style=\"padding-left:0px;padding-right:0px;\">");
				sb.Append(sep.toOutString());
				sb.Append("</td>");
				sb.Append("<td style=\"padding-left:0px;padding-right:0px;\">");
				sb.Append("當前顯示記錄 %1$s - %2$s 總計 %3$s",paging.getBegin(),paging.getEnd(),paging.getTotalRows());
				sb.Append("</td>");
				
				sb.Append("</tr></tbody></table>");
				sb.Append("</td>");
				
				sb.Append("</tr></tbody></table>");
				sb.Append("</th></tr>");
				try {
					writer.write(sb.toString());
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
		super.onOutItemEnd(writer);
	}
	
}
